#!/usr/bin/env python
# encoding: utf-8
"""
@Author: Beam
@Mail:506556658@qq.com
@file: login.py.py
@time: 2017/8/10 10:24
"""

from django.shortcuts import render,HttpResponse,redirect
from cmdb.models import User
from cmdb.models import BookInfo

def login(request):
    if request.method == 'GET':
        return render(request,'login.html')
    elif request.method == 'POST':
        user = request.POST.get('username',None)
        password = request.POST.get('password', None)
        try:
            dbuser = User.objects.get(username = user)
            if dbuser.password == password:
                return redirect('/home')
            else:
                err_msg = '密码错误'
                return render(request,'login.html',{'err_msg':err_msg})
        except:
            err_msg = '该用户不存在'
            return render(request, 'login.html', {'err_msg': err_msg})
    else:
        return HttpResponse('404')


def home(request):
    if request.method == 'POST':
        bookname = request.POST.get('bookname')
        author = request.POST.get('author')
        price = request.POST.get('price')
        date = request.POST.get('date')
        bookinfo = BookInfo()
        bookinfo.bookname = bookname
        bookinfo.author = author
        bookinfo.price = price
        bookinfo.date = date
        bookinfo.save()
    booklist = BookInfo.objects.all()
    return render(request,'projects.html',{'booklists':booklist})